module adialog
import StdEnv, japi

/* This program exemplifies how to create a modal dialog
   using the function jshow (that shows the dialog),
   jhide (that hides it), jDisable (that disables the
   frame when one opens the dialog), and jEnable.
   Attention! Do not forget to add the Spartan shield 
   icon to the folder containing the application. */

:: State = {fr::!Framer, fd::!TDev, dg::!Container} 

fat n
  | n < 1= 1
  | otherwise= n* (fat (n-1))
  
strfat txt= (jsetText txt (toString(fat (toInt (jgetText txt)))))  

Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame (WH  400 300) "Factorial"  js
    (spartan, js)= jloadimage "lambda.jpg" js
    (frm, js) = jseticon frm spartan js 
    (txt, js) = jnewField (XYWH 80 200 200 40) 30 frm js
    (txt, js) = bg_namedcolor J_YELLOW (txt,  js)
    (dlg, js) = jnewDialog (XY 30 60) "Help" frm js
    (dlg, js) = jverticalLayout dlg js
    (_, js)= jnewLabel FLOW "To calculate the factorial," dlg js
    (_, js)= jnewLabel FLOW "type a number on the editor," dlg  js
    (_, js)= jnewLabel  FLOW "then press <Enter>." dlg js
    (ok, js) = jnewButton  FLOW "Ok" dlg js
    
    (dlg, js) = jpack dlg js 
    (mnb, js) = jnewBar frm js
    (filemn, js) = jnewMenu "File" mnb js 
    (opn, js) = jnewItem "Open" filemn  js
    (qt, js) = jnewItem "Quit" filemn  js
    (frm, js) = jshow frm js
    st= {fr= frm, fd= txt, dg= dlg}
    js= loop st js
        with
           loop :: !State !JapiState -> JapiState
           loop st =: {fr= frm} js
              # (na, js) = jnextaction js
              | na == (act frm) || na== (act qt) = jquit js
              | na== (act opn)
                  # (dlg, js)= jshow st.dg js
                  # (frm, js)= jDisable frm js
                  = loop {st & fr= frm, dg= show dlg} js
              | na== (act ok)
                  # (dlg, js)= jhide st.dg js
                  # (frm, js)= jEnable frm js
                  = loop {st & fr= frm, dg= hide dlg} js 
              | na== (act txt) = loop {st & fd= strfat txt} js
              | otherwise= loop st js
  = end_japi js world

    
    
 